package com.yuezhi.acl.service.impl;

import com.yuezhi.acl.entity.SysUser;
import com.yuezhi.acl.security.entity.SecurityUser;
import com.yuezhi.acl.security.entity.User;
import com.yuezhi.acl.service.SysPermissionService;
import com.yuezhi.acl.service.SysUserService;
import com.yuezhi.exception.YueZhiException;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: yuezhi-parent
 * @description:
 * @author: Mr.Liao
 * @create: 2020-12-16 14:22
 **/
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Resource
    private SysUserService userService;

    @Resource
    private SysPermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询数据
        SysUser user = userService.selectByUsername(username);
        //判断
        if(user == null){
            throw new YueZhiException(201,"用户名或密码错误！");
        }
        User curUser = new User();
        BeanUtils.copyProperties(user,curUser);
        //根据用户查询用户权限列表
        List<String> list = permissionService.selectPermissionValueByUserId(user.getId());
        SecurityUser securityUser=new SecurityUser();
        securityUser.setCurrentUserInfo(curUser);
        securityUser.setPermissionValueList(list);
        return securityUser;
    }
}
